/*============================================================================*/
/*                   CONTINENTAL AUTOMOTIVE SYSTEMS                           */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* name:            MALPIT_Init.c
* version:         1.1
* created_by:      Lizette Acosta/Rodrigo Casados
* date_created:    June 22 2013
*=============================================================================*/
/* DESCRIPTION : Initializes ports                                            */
/*============================================================================*/
/* FUNCTION COMMENT : Initializes IRQ vectors, PIT and disables Watchdog      */
/* application stuff.                                                         */
/*                                                                            */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.1      | 22/June/2013 |                              | Lizette Acosta   */
/*                                                           Rodrigo Casados  */
/*============================================================================*/



/*****************************************************************************************************
* Include files
*****************************************************************************************************/

/* Data types */
#include "typedefs.h"
/* Own Headers */



/*****************************************************************************************************
* Definition of module wide (CONST-) CONSTANTs 
*****************************************************************************************************/

/*****************************************************************************************************
* Code of module wide FUNCTIONS
*****************************************************************************************************/

/****************************************************************************************************/
/**
* \brief    PIT device - Low level initialization
* \author   Abraham Tezmol
* \param    void
* \return   void
*/
void PIT_device_init(void) 
{
    PIT.PITMCR.R = 0x00000001;       /* Enable PIT and configure timers to stop in debug mode */
}

/****************************************************************************************************/
/**
* \brief    PIT Channel Low level configuration
* \author   Abraham Tezmol
* \param    uint8_t channel - Channel to be configured
* \param    tCallbackFunction Callback - Function to invoke upon PIT count to zero event
* \return   void
*/
void PIT_channel_configure(uint8_t channel, tCallbackFunction Callback) 
{
    PIT.CH[channel].LDVAL.R = PIT_CHANNEL_VALUE;      /* PIT1 timeout --> Refer to PIT.h file for calculations */
    PIT_Callback[channel]   = Callback;               /* Initialize callback function */
    
    /* Install Interrupt routine for this specific channel */
    INTC_InstallINTCInterruptHandler(PIT_channel_0_isr,59,5);
}

/****************************************************************************************************/
/**
* \brief    PIT channel Low level Start procedure
* \author   Abraham Tezmol
* \param    uint8_t channel - PIT channel to start
* \return   void
*/
void PIT_channel_start(uint8_t channel)
{
    PIT.CH[channel].TCTRL.R = 0x000000003;            /* Enable PIT1 interrupt and make PIT active to count */
}


/****************************************************************************************************/
/**
* \brief    PIT channel Low level Stop procedure
* \author   Abraham Tezmol
* \param    uint8_t channel - PIT channel to stop
* \return   void
*/
void PIT_channel_stop(uint8_t channel)
{
	PIT.CH[channel].TCTRL.R = 0x000000000;            /* Disable PIT channel interrupt and make PIT inactive to count */
}





void PIT_channel_0_isr(void)
{
  if (PIT.CH[0].TFLG.B.TIF)
  {
    /* Clear the time-base interrupt flag */
    PIT.CH[0].TFLG.B.TIF = 1;
    
    /*call callback function, if initialized*/
        if( PIT_Callback[0] != NULL )
        {
            PIT_Callback[0]();
        }
  }
}

void enableIrq(void) {
  INTC.CPR.B.PRI = 0;          /* Single Core: Lower INTC's current priority */
  asm(" wrteei 1");	    	   /* Enable external interrupts */
}



void initModesAndClock(void) 
{
	ME.MER.R = 0x0000001D;          	/* Enable DRUN, RUN0, SAFE, RESET modes */
	                              		/* Initialize PLL before turning it on: */
	CGM.FMPLL_CR.R = 0x02400100;    	/* 8 MHz xtal: Set PLL0 to 64 MHz */   

	ME.RUN[0].R = 0x001F0074;       	/* RUN0 cfg: 16MHzIRCON,OSC0ON,PLL0ON,syclk=PLL */
	ME.RUNPC[1].R = 0x00000010; 		/* Peri. Cfg. 1 settings: only run in RUN0 mode */	

	ME.PCTL[92].R = 0x01;           	/* PIT, RTI: select ME_RUN_PC[1] */
	ME.PCTL[68].R = 0x01;				/* SIUL */
	ME.PCTL[32].R = 0x01; 	      		/* MPC56xxB/P/S ADC 0: select ME.RUNPC[1] */
		    		
	                              		/* Mode Transition to enter RUN0 mode: */
	ME.MCTL.R = 0x40005AF0;         	/* Enter RUN0 Mode & Key */
	ME.MCTL.R = 0x4000A50F;         	/* Enter RUN0 Mode & Inverted Key */  
	while (ME.GS.B.S_MTRANS) {}     	/* Wait for mode transition to complete */    
	                              		/* Note: could wait here using timer and/or I_TC IRQ */
	while(ME.GS.B.S_CURRENTMODE != 4) 	/* Verify RUN0 is the current mode */
	{;} 								
}

void disableWatchdog(void) 
{
  SWT.SR.R = 0x0000c520;     /* Write keys to clear soft lock bit */
  SWT.SR.R = 0x0000d928; 
  SWT.CR.R = 0x8000010A;     /* Clear watchdog enable (WEN) */
}

